/**
 * <p>Package : org.tea.springdata.web.config</p>
 * <p>E-Mail : ztlyde@gmail.com</p>
 */
package org.tea.springdata.web.config;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.servlet.view.xml.MarshallingView;

/**
 * Spring mvc配置
 * @author: John zhang
 * @version: 0.1
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "org.tea.springdata.web.controller" }, excludeFilters = { @Filter(Configuration.class) })
public class WebConfig extends WebMvcConfigurerAdapter {

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		configurer.mediaTypes(new HashMap<String, MediaType> () {
			private static final long serialVersionUID = 1L;
			{
				put( "json", MediaType.APPLICATION_JSON );
				put( "xml", MediaType.APPLICATION_XML );
			}
		});
		configurer.defaultContentType( MediaType.APPLICATION_JSON );
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
	}
	
	@Bean
	public ViewResolver contentNegotiatingViewResolver() {
		ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
		viewResolver.setDefaultViews(new ArrayList<View> () {
			private static final long serialVersionUID = 1L;
			{
				add( new MappingJackson2JsonView() );
				
				XStreamMarshaller marshaller = new XStreamMarshaller();
				marshaller.setAutodetectAnnotations(true);
				add( new MarshallingView(marshaller) );
			}
		});
		
		viewResolver.setViewResolvers(new ArrayList<ViewResolver> () {
			private static final long serialVersionUID = 1L;
			{	
				InternalResourceViewResolver internalViewResolver = new InternalResourceViewResolver();
				internalViewResolver.setPrefix("/WEB-INF/views");
				internalViewResolver.setSuffix(".jsp");
				add( internalViewResolver );
			}
		});
		return viewResolver;
	}
	
}
